package cn.flying.cloud.file.core.service;

import javax.annotation.Resource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.oss.exception.OssException;
import cn.flying.cloud.file.bo.FileInfoBo;
import cn.flying.cloud.file.bo.FileStorageBo;
import cn.flying.cloud.file.model.FileRequest;
import cn.flying.cloud.file.service.FileInfoService;
import cn.flying.cloud.file.service.FileStorageService;
import cn.flying.cloud.system.dict.service.DictDataService;

/**
 * @author: admin
 * @date: 2025年04月05日 14:08
 * @version: 1.0
 */
@Service
public class FileService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private FileStorageService fileStorageService;
    @Resource
    private OssService ossService;
    @Resource
    private DictDataService dictDataService;

    /**
     * 上传文件
     *
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String uploadFile(FileRequest request) {
        try {
            // 将 byte 数组转换为 InputStream
            InputStream inputStream = new ByteArrayInputStream(request.getContent());
            String fileName = request.getFileName();
            String bucketName = request.getBucketName();
            String tenantId = request.getTenantId();
            String objectName = ossService.putObject(tenantId, bucketName, fileName, inputStream);

            FileInfoBo info = new FileInfoBo();
            info.setRequestNo(request.getRequestNo());
            info.setServiceCode(request.getServiceCode());
            Map<String, String> serviceNames = dictDataService.getDict("service_name");
            info.setServiceName(serviceNames.get(request.getServiceCode()));
            info.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
            info.setFileName(fileName);
            info.setFileUrl("");
            info.setBucketName(bucketName);
            info.setBucketFile(bucketName + "/" + objectName);

            FileStorageBo param = new FileStorageBo();
            param.setTenantId(tenantId);
            param.setIsDefault("Y");
            FileStorageBo storage = fileStorageService.getOneObj(param);
            info.setVendorCode(storage.getVendorCode());
            info.setVendorName(storage.getVendorName());
            info.setTenantId(tenantId);

            fileInfoService.createObj(info);
            return bucketName + "/" + objectName;
        } catch (Exception e) {
            log.error("上传文件失败！", e);
            throw e;
        }
    }

    /**
     * 下载文件
     *
     * @param bucket
     * @param filename
     */
    public byte[] download(String bucket, String filename) {
        try {
            String tenantId = Ft.Local.TenantId();
            return ossService.getObjectBytes(tenantId, bucket, filename);
        } catch (Exception e) {
            throw new OssException("文件下载失败！", e);
        }
    }

    /**
     * 获取Url
     *
     * @param bucket
     * @param filename
     * @return
     */
    public String getObjectUrl(String bucket, String filename) {
        String tenantId = Ft.Local.TenantId();
        return ossService.getObjectUrl(tenantId, bucket, filename, 1, TimeUnit.DAYS);
    }
}
